package com.ewanghuiju.app.widget.popupwindow;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.model.bean.local.WalletClassifyBean;
import com.ewanghuiju.app.ui.my.adapter.WalletClassifyFlexboxAdapter;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import razerdp.basepopup.BasePopupWindow;

/**
 * @author sj  qq:751943509
 * @class 钱包分类的弹框
 * @time 2020/2/19
 */
public class WalletClassifyPopup extends BasePopupWindow {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    @BindView(R.id.layout_main)
    LinearLayout layoutMain;

    WalletClassPopupCallback walletClassPopupCallback;

    public WalletClassifyPopup(Context context, WalletClassPopupCallback walletClassPopupCallback) {
        super(context);
        setWidth(getScreenWidth());
        ButterKnife.bind(this, getContentView());
        this.walletClassPopupCallback = walletClassPopupCallback;
    }

    public void setRecycleView(List<WalletClassifyBean> list) {

        FlexboxLayoutManager manager = new FlexboxLayoutManager(getContext(), FlexDirection.ROW, FlexWrap.WRAP) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };

        recyclerView.setLayoutManager(manager);
        WalletClassifyFlexboxAdapter adapter = new WalletClassifyFlexboxAdapter(R.layout.item_walletclassify_flowlayout);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).setSelected(position == i);
                }
                adapter.notifyDataSetChanged();
                if (walletClassPopupCallback != null) {
                    walletClassPopupCallback.onClickCallback(position);
                }
                dismiss();
            }
        });
        recyclerView.setAdapter(adapter);
        adapter.setNewData(list);
    }


    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.popup_wallet_classify);
    }

    public interface WalletClassPopupCallback {
        //确认按钮
        void onClickCallback(int position);
    }
}
